//
//  YLALodaingView.swift
//  Your Lover AI
//
//  Created by 阿忠 on 2024/7/30.
//

import Foundation
import UIKit

/**
 修改名字的弹窗
 */
class YLALodaingView: UIView {
    var progressView: UIProgressView!
    
    var timer: Timer?
    
    
    lazy var centerLb: UILabel = {
        let lab = UILabel()
        lab.text = "Loading, please wait…"
        lab.textColor = .white
        lab.font = UIFont.systemFont(ofSize: 16)
        lab.textAlignment = .center
        lab.numberOfLines = 0
        return lab
    }()
    
    lazy var bgImg: UIImageView = {
        let image = UIImageView(image: UIImage(named: "img_launch"))
        return image
    }()
    
    lazy var logoImg: UIImageView = {
        let image = UIImageView(image: UIImage(named: "logo"))
        return image
    }()
    
    lazy var proVc: UIView = {
        let vc = UIView()
        vc.backgroundColor = .clear
        vc.layer.cornerRadius = 10.0
        vc.layer.borderWidth = 2
        vc.layer.borderColor = UIColor.white.cgColor
        vc.layer.masksToBounds = true
        return vc
    }()
    
    var onCancel: (() -> Void)?
    var onConfirm: ((String?) -> Void)?
    
    init(frame: CGRect, title: String, paymentMethod: String, paymentDescription: String) {
        super.init(frame: frame)
        
        setupUI(title: title, paymentMethod: paymentMethod, paymentDescription: paymentDescription)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(title: String, paymentMethod: String, paymentDescription: String) {
        // 设置自定义弹窗的样式和布局
        self.backgroundColor = .white
     
        self.addSubview(bgImg)
        bgImg.snp.makeConstraints { make in
            make.left.top.bottom.right.equalToSuperview()
        }
        
        self.addSubview(logoImg)
        logoImg.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(206)
            make.width.height.equalTo(96)
        }
        
        self.addSubview(proVc)
        proVc.snp.makeConstraints { make in
            make.bottom.equalTo(-148)
            make.left.equalTo(62)
            make.right.equalTo(-62)
            make.height.equalTo(20)
        }
        
        self.addSubview(centerLb)
        centerLb.snp.makeConstraints { make in
            make.bottom.equalTo(proVc.snp.top).offset(-11)
            make.centerX.equalToSuperview()
        }
        
        // 设置进度条的颜色为白色
        progressView = UIProgressView()
        progressView.progressTintColor = .white
        // 如果需要的话，设置轨道颜色（默认是灰色）
        progressView.trackTintColor = UIColor.clear // 或者设置为其他颜色
        self.addSubview(progressView)
        progressView.snp.makeConstraints { make in
            make.centerY.equalTo(proVc)
            make.left.equalTo(67)
            make.right.equalTo(-67)
            make.height.equalTo(11)
        }
        
    
                
        // 开始加载进度条
        startLoadingProgress()
        
    }
    
    func startLoadingProgress() {
            // 进度条的初始值设为0
            progressView.progress = 0.0
            
            // 创建一个定时器，每秒更新一次进度（总共8秒）
        timer = Timer.scheduledTimer(timeInterval: 0.5, target: self, selector: #selector(updateProgress), userInfo: nil, repeats: true)
            
            // 8秒后停止定时器并触发完成方法
            DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
                self.stopLoadingProgress()
                self.progressCompleted() // 调用完成方法
            }
        }
    
    @objc func updateProgress() {
            // 更新进度，直到达到100%
            if progressView.progress < 1.0 {
                progressView.progress += 0.5 // 8秒 = 8次 * 1秒/次，所以每次增加1/8 = 0.125
            }
        }
        
        func stopLoadingProgress() {
            // 停止定时器
            timer?.invalidate()
            timer = nil
            
            // 如果需要，可以将进度条设置为100%（通常UI会自动更新到100%）
            // progressView.progress = 1.0
        }
        
        func progressCompleted() {
            
            // 进度条加载完成，触发的方法
            print("进度条加载完成")
            // 在这里执行你需要的操作
            onCancel?()
            
   
        }

}


